inline GnDX9DeviceDesc::DisplayFormatInfo::RenderTargetInfo::RenderTargetInfo( D3DFORMAT format )
	: mRenderTargetFormat(format)
{
	memset( maDepthStencil, D3DFMT_UNKNOWN, sizeof(maDepthStencil) );
}

inline bool GnDX9DeviceDesc::DisplayFormatInfo::RenderTargetInfo::operator==(D3DFORMAT& format) const
{
	return ( format == mRenderTargetFormat );
}

inline bool GnDX9DeviceDesc::DisplayFormatInfo::operator==(DisplayFormatInfo& p) const
{
	return  (p.mFormat == mFormat );
}

inline bool GnDX9DeviceDesc::DisplayFormatInfo::operator==(D3DFORMAT& format) const
{
	return ( format == mFormat );
}

inline D3DFORMAT GnDX9DeviceDesc::DisplayFormatInfo::GetFormat()
{
	return mFormat;
}

inline bool GnDX9DeviceDesc::DisplayFormatInfo::IsRenderWindowed()
{
	return mIsWindowed;
}

inline bool GnDX9DeviceDesc::DisplayFormatInfo::IsRenderFullScreen()
{
	return mIsFullScreen;
}

inline GnDX9DeviceDesc::DisplayFormatInfo* GnDX9DeviceDesc::GetDisplay( D3DFORMAT format )
{
	DisplayFormatInfo* info = NULL;
	GnTHelper<DisplayFormatInfo*>::FindVector(mDisplayFormats, format, info);
	return info;
}

inline const D3DCAPS9& GnDX9DeviceDesc::GetCaps() const
{
	return mD3DCaps9;
}

inline guint GnDX9AdapterDesc::ModeDesc::GetClosestRefreshRate(guint refreshRate) const
{
	gsize size = mRefreshRates.size();
	GnAssert( size );

	guint testRefreshRate = mRefreshRates[0];
	guint bestRefreshDiff = abs((int)testRefreshRate - (int)refreshRate);
	guint closestRefreshRate = D3DPRESENT_RATE_DEFAULT;
	for ( guint i = 1 ; i < size ; i++ )
	{
		testRefreshRate = mRefreshRates[i];

		unsigned int uiRefreshDiff  = abs((int)testRefreshRate - (int)refreshRate);

		if (uiRefreshDiff < bestRefreshDiff)
		{
			bestRefreshDiff = uiRefreshDiff;
			closestRefreshRate = testRefreshRate;
		}
	}
	return closestRefreshRate;
}
inline GnDX9DeviceDesc* GnDX9AdapterDesc::GetDeviceDesc( D3DDEVTYPE devType )
{
	if( devType == D3DDEVTYPE_HAL )
		return mpHALDeviceDesc;
	else
		return mpREFDeviceDesc;

	return NULL;
}

inline GnDX9AdapterDesc* GnDX9SystemDesc::GetAdapterDesc( guint numAdapter )
{
	gsize size = mAdapterList.size();
	GnAssert( numAdapter < size );
	return mAdapterList[numAdapter];
}

inline guint GnDX9SystemDesc::GetBPPFromFormat(D3DFORMAT d3dFormat)
{
	switch (d3dFormat)
	{
	case D3DFMT_UNKNOWN:
		return 0;

	case D3DFMT_VERTEXDATA:
		return 0;

		// FOURCC formats
	case D3DFMT_UYVY:
	case D3DFMT_R8G8_B8G8:
	case D3DFMT_YUY2:
	case D3DFMT_G8R8_G8B8:
	case D3DFMT_DXT1:
	case D3DFMT_DXT2:
	case D3DFMT_DXT3:
	case D3DFMT_DXT4:
	case D3DFMT_DXT5:
	case D3DFMT_MULTI2_ARGB8:
		return 0;

	case D3DFMT_R3G3B2:
	case D3DFMT_A8:
	case D3DFMT_P8:
	case D3DFMT_L8:
	case D3DFMT_A4L4:
		return 8;

	case D3DFMT_R5G6B5:
	case D3DFMT_X1R5G5B5:
	case D3DFMT_A1R5G5B5:
	case D3DFMT_A4R4G4B4:
	case D3DFMT_A8R3G3B2:
	case D3DFMT_X4R4G4B4:
	case D3DFMT_G16R16:
	case D3DFMT_A8P8:
	case D3DFMT_A8L8:
	case D3DFMT_V8U8:
	case D3DFMT_L6V5U5:
	case D3DFMT_D16_LOCKABLE:
	case D3DFMT_D15S1:
	case D3DFMT_D16:
	case D3DFMT_L16:
	case D3DFMT_INDEX16:
	case D3DFMT_R16F:
	case D3DFMT_CxV8U8:
		return 16;

	case D3DFMT_R8G8B8:
		return 24;

	case D3DFMT_A8R8G8B8:
	case D3DFMT_X8R8G8B8:
	case D3DFMT_A2B10G10R10:
	case D3DFMT_A8B8G8R8:
	case D3DFMT_X8B8G8R8:
	case D3DFMT_A2R10G10B10:
	case D3DFMT_X8L8V8U8:
	case D3DFMT_Q8W8V8U8:
	case D3DFMT_V16U16:
	case D3DFMT_A2W10V10U10:
	case D3DFMT_D32:
	case D3DFMT_D24S8:
	case D3DFMT_D24X8:
	case D3DFMT_D24X4S4:
	case D3DFMT_D32F_LOCKABLE:
	case D3DFMT_D24FS8:
	case D3DFMT_INDEX32:
	case D3DFMT_G16R16F:
	case D3DFMT_R32F:
		return 32;

	case D3DFMT_A16B16G16R16:
	case D3DFMT_Q16W16V16U16:
	case D3DFMT_A16B16G16R16F:
	case D3DFMT_G32R32F:
		return 64;

	case D3DFMT_A32B32G32R32F:
		return 128;

	default:
		return 0xFFFFFFFF;
	}
}

inline D3DFORMAT GnDX9SystemDesc::GetCompatibleAdapterFormat(D3DFORMAT d3dFormat)
{
	switch (d3dFormat)
	{
	case D3DFMT_UNKNOWN:
	case D3DFMT_A8:
	case D3DFMT_A8R3G3B2:
		return D3DFMT_UNKNOWN;
	case D3DFMT_R8G8B8:
		return D3DFMT_R8G8B8;
	case D3DFMT_A8R8G8B8:
	case D3DFMT_X8R8G8B8:
		return D3DFMT_X8R8G8B8;
	case D3DFMT_R5G6B5:
		return D3DFMT_R5G6B5;
	case D3DFMT_X1R5G5B5:
	case D3DFMT_A1R5G5B5:
		return D3DFMT_X1R5G5B5;
	case D3DFMT_X4R4G4B4:
	case D3DFMT_A4R4G4B4:
		return D3DFMT_X4R4G4B4;
	case D3DFMT_R3G3B2:
		return D3DFMT_R3G3B2;
	}

	return D3DFMT_UNKNOWN;
}

inline void GnDX9SystemDesc::GetDepthStencilBPPFromFormat( D3DFORMAT d3dFmt,
	guint& depthBPP, guint& stencilBPP )
{
	switch (d3dFmt)
	{
	case D3DFMT_D16_LOCKABLE:
		depthBPP = 16;
		stencilBPP = 0;
		break;
	case D3DFMT_D24S8:
		depthBPP = 24;
		stencilBPP = 8;
		break;
	case D3DFMT_D32:
		depthBPP = 32;
		stencilBPP = 0;
		break;
	case D3DFMT_D15S1:
		depthBPP = 15;
		stencilBPP = 1;
		break;
	case D3DFMT_D16:
		depthBPP = 16;
		stencilBPP = 0;
		break;
	case D3DFMT_D24X8:
		depthBPP = 24;
		stencilBPP = 0;
		break;
	case D3DFMT_D24X4S4:
		depthBPP = 24;
		stencilBPP = 4;
		break;
	case D3DFMT_UNKNOWN:
	default:
		depthBPP = 0;
		stencilBPP = 0;
		break;
	}
}
